// pages/topic/topic.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [{
      id: 0,
      type: 'image',
      src: 'http://a4.qpic.cn/psb?/V10uH3z53mPeWp/QYqJIPoz8Hqxhg6m2*9Y0pwQSUoO1Y9tVM.Li7PghAE!/b/dL8AAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDV3I!&tl=1&vuin=939682389&tm=1556618400&sce=60-1-1&rf=viewer_4',
      url: "/pages/mine/contact/contact"},
      {
        id: 1,
        type: 'image',
        src: 'http://a3.qpic.cn/psb?/V10uH3z53mPeWp/psJb1QTurv5rW5412OYbgCZe5WDwdy0.K.8Cy7iN624!/b/dLYAAAAAAAAA&ek=1&kp=1&pt=0&bo=fwQ.AQAAAAADN1Y!&tl=1&vuin=939682389&tm=1557817200&sce=60-4-3&rf=viewer_4',
        url: "/pages/mine/contact/contact"
      },],
      TabCur: 0,
    scrollLeft: 0,
    items:["热点","活动"],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_show: false,
    list:[
      [],
      []
    ],
    imglist:[
      {
        url:'http://a1.qpic.cn/psb?/V10uH3z53mPeWp/ym*2O6FwvdjHnyfxItLTm3Kvz9ONOCO9nFrPt0WRw1w!/b/dIQAAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDNxI!&tl=1&vuin=939682389&tm=1556802000&sce=60-1-1&rf=viewer_4',
        name:'运动'
      },
      {
        url:'http://a1.qpic.cn/psb?/V10uH3z53mPeWp/ZD*PaLXe8hWrzTx5GogezC*3sv8nAA*zaxKOOy02LX8!/b/dMQAAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDZ0I!&tl=1&vuin=939682389&tm=1556802000&sce=60-1-1&rf=viewer_4',
        name:"音乐"
      },{
        url:'http://m.qpic.cn/psb?/V10uH3z53mPeWp/7JmpDREqXXyD5f*J93vBFl0lC*MK0aJq4ao9FcSD.1Y!/b/dFIBAAAAAAAA&bo=gAdYAoAHWAIDByI!&rf=viewer_4',
        name:'庆祝'
      },{
        url:'http://a4.qpic.cn/psb?/V10uH3z53mPeWp/jqCPmJySf96eJ*N0GilyMCpgIpCOUP*Ojqzfk*K7G04!/b/dL8AAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDR2I!&tl=1&vuin=939682389&tm=1556892000&sce=60-1-1&rf=viewer_4',
        name:'学习'
      },
      {
        url:"http://a4.qpic.cn/psb?/V10uH3z53mPeWp/jyRmnRr7itfv7y9AGDi2J.fJ*x8PB*vH*f1WLK.cSSA!/b/dL8AAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAoAHWAIDh6I!&tl=1&vuin=939682389&tm=1556935200&sce=60-1-1&rf=viewer_4",
        name:'吐槽'
      },{
        url:'http://m.qpic.cn/psb?/V10uH3z53mPeWp/PG9S5kfZsNmNLkjsxCFfuxONYr.zUNBvu4Jjzj*s03E!/b/dLYAAAAAAAAA&bo=gAdYAoAHWAIDByI!&rf=viewer_4',
        name:'创意'
      },{
        url:"http://a1.qpic.cn/psb?/V10uH3z53mPeWp/LGp.s*X*PMyoUPMYrisyaWGE2DmuVVPnSqIE6rEvz2Q!/b/dMAAAAAAAAAA&ek=1&kp=1&pt=0&bo=gAdYAgAAAAADZ58!&tl=1&vuin=939682389&tm=1557806400&sce=60-1-1&rf=viewer_4",
        name:"运动会女"
      },{
        url:"http://m.qpic.cn/psb?/V10uH3z53mPeWp/Hil5ZBCg5*PE5xcSuZDVLKitSMkte3tyV8pBGCNiBC0!/b/dL8AAAAAAAAA&bo=gAdYAgAAAAADB*8!&rf=viewer_4",
        name:"运动会男"
      }
    ], 
    page:1,
    isMore:false,
    isLoad:false,
    isPopping: false,//是否已经弹出  
    animPlus: {},//旋转动画  
    animCollect: {},//item位移,透明度  
    animTranspond: {},//item位移,透明度  
    animInput: {},//item位移,透明度  
    is_load:true
    }, 
  getTopicList(page){
    var that = this
    if (that.data.isMore) {
      return
    }
    that.setData({
      isLoad: true
    })
    wx.request({
      url: app.globalData.url + 'topic', // 仅为示例，并非真实的接口地址
      data: {
        page: page,
        type:0
      },
      method: "GET",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.data.length == 0) {
          that.setData({
            isMore: true,
            isLoad: false
          })
          return 
        }
        var topic = that.data.list
        for (var i=0;i<res.data.data.length;i++){
          topic[0].push(res.data.data[i])
        }
        that.setData(
          {
            list: topic,
            page: page+1,
            isLoad: false
          }
        )
      }
    })

  },
  toDeatail(e){
    var that =this
    console.log(e.currentTarget.dataset.id)
    //var topic_id = that.data.list[that.data.TabCur][e.currentTarget.dataset.id].id
    let str = JSON.stringify(that.data.list[that.data.TabCur][e.currentTarget.dataset.id]);
    wx.navigateTo({
      url: './detail/detail?topic=' + str,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  bindGetUserInfo(e){
    var that = this;
    that.onLoad()
    if (e.detail.userInfo) {
      console.log(e.detail.userInfo.nickName)
      app.globalData.userInfo = e.detail.userInfo
      wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          wx.request({
            url: app.globalData.url+'user/code',
            data: {
              code: res.code,
              nick: e.detail.userInfo.nickName,
              avatar: e.detail.userInfo.avatarUrl
            },
            method: "POST",
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              app.globalData.openid = res.data.data.openid //返回openid
              console.log("------openid------")
              console.log(res.data.data)
            }
          })
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    this.setData({
      isMore:false,
      isLoad:false,
      page:1,
      list: [
        [],
        []
      ],
      authority_id: app.globalData.userDetail.authority_id
    })
    var that = this ;
    that.setData(
      {
        is_load:false
      }
    )
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success(res) {
              console.log(res.userInfo)
 
            }
          })
        
          that.setData({
            is_show: false,
             is_load: true
          })
        }
        else{
          that.setData({
            is_show:true,
            is_load: true
          })
        }
      }
    })
    this.getTopicList(that.data.page)
  },
  showModal(e) {
    this.setData({
      is_show: e.currentTarget.dataset.target
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad()
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('--------上拉触底-------')
    var that = this;
    that.getTopicList(that.data.page)
   
    
    //console.log(pag)
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  plus: function () {
    if (this.data.isPopping) {
      //缩回动画  
      this.popp();
      this.setData({
        isPopping: false
      })
    } else if (!this.data.isPopping) {
      //弹出动画  
      this.takeback();
      this.setData({
        isPopping: true
      })
    }
  },
  input: function () {
    console.log("input")
  },
  toUpdate: function () {
    console.log("toUpdate")
    wx.navigateTo({
      url: './publish/publish',
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  collect: function () {
    console.log("collect")
  },
  //弹出动画  
  //弹出动画
  popp: function () {
    //plus顺时针旋转
    var animationPlus = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationcollect = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationTranspond = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationInput = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    animationPlus.rotateZ(180).step();
    animationcollect.translate(-40, -40).rotateZ(180).opacity(1).step();
    animationTranspond.translate(-70, 0).rotateZ(180).opacity(1).step();
    animationInput.translate(-50, 50).rotateZ(180).opacity(1).step();
    this.setData({
      animPlus: animationPlus.export(),
      animCollect: animationcollect.export(),
      animTranspond: animationTranspond.export(),
      animInput: animationInput.export(),
    })
  },
  //收回动画
  takeback: function () {
    //plus逆时针旋转
    var animationPlus = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationcollect = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationTranspond = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    var animationInput = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-out'
    })
    animationPlus.rotateZ(0).step();
    animationcollect.translate(0, 0).rotateZ(0).opacity(0).step();
    animationTranspond.translate(0, 0).rotateZ(0).opacity(0).step();
    animationInput.translate(0, 0).rotateZ(0).opacity(0).step();
    this.setData({
      animPlus: animationPlus.export(),
      animCollect: animationcollect.export(),
      animTranspond: animationTranspond.export(),
      animInput: animationInput.export(),
    })
  },


})